#if !defined(AFX_PLAINTEXTFORMAT_H__20000619_217E_7B35_209A_0080AD509054__INCLUDED_)
#define AFX_PLAINTEXTFORMAT_H__20000619_217E_7B35_209A_0080AD509054__INCLUDED_
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2000.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CPlainTextFormat
{
public:
   static CString Format(LPCTSTR pstrText)
   {
      if( pstrText==NULL ) return _T(""); // Not uncommon in case of BSTR "NULL"

      CString s;
      DWORD dwSize = _tcslen(pstrText)*8;
      if( dwSize<4000 ) dwSize=8000;
      LPTSTR pstrTarget = s.GetBuffer(dwSize);
      LPTSTR pEnd = pstrTarget + dwSize - 256;
      LPTSTR pT = pstrTarget;

      TCHAR ch;
      while( (ch = *pstrText++)!=_T('\0') ) {
         switch( ch ) {
         case _T('{'):
            *pT++ = _T('\\');
            *pT++ = _T('{');
            break;
         case _T('}'):
            *pT++ = _T('\\');
            *pT++ = _T('}');
            break;
         case _T('\\'):
            *pT++ = _T('\\');
            *pT++ = _T('\\');
            break;
         case _T('\r'):
            break;
         case _T('\n'):
            *pT++ = _T('\\');
            *pT++ = _T('p');
            *pT++ = _T('a');
            *pT++ = _T('r');
            *pT++ = _T(' ');
            break;
         default:
            *pT++ = ch;
         }

         if( pT > pEnd ) break; // TODO: Add intelligent string growth
      };
      *pT = _T('\0');
      s.ReleaseBuffer();
      return s;
   };

};



#endif // !defined(AFX_PLAINTEXTFORMAT_H__20000619_217E_7B35_209A_0080AD509054__INCLUDED_)

